package com.xindaoapp.happypet.adapter;

import java.util.LinkedList;
import java.util.List;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.bean.ClubC;

public class AttentionAdapter extends BaseAdapter {
    private final Context context;
    private final List<ClubC> pets;
    public boolean longclick = false;
    private final SharedPreferences sp;

    public AttentionAdapter(Context context, LinkedList<ClubC> attention) {
        super();
        this.context = context;
        this.pets = attention;
        sp = context.getSharedPreferences("menuconfig", Context.MODE_PRIVATE);
    }

    @Override
    public int getCount() {
        return pets.size();
    }

    public void addItem(List<ClubC> top) {
        pets.addAll(top);
    }

    public void clearItem() {
        if (pets != null)
            pets.clear();
    }



    @Override
    public Object getItem(int position) {
        return pets.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View view = View.inflate(context, R.layout.attention_top_item, null);
        ImageView imageView = (ImageView) view.findViewById(R.id.iv_item_list);
        TextView textView = (TextView) view.findViewById(R.id.tv_item_list);
        if (position == pets.size() - 1) {
            imageView.setImageResource(R.drawable.left_menu_add);
        } else {
            if (pets.size() > 0) {
                try {
                    // imageView.setImageResource(pets.get(position).getId());
                    if (!TextUtils.isEmpty(pets.get(position).getPicUrl())) {
                        ImageLoader.getInstance().displayImage(pets.get(position).getPicUrl(),
                                imageView);
                    } else {
                        imageView.setBackgroundResource(R.drawable.picture_loading);
                    }
                    textView.setText(pets.get(position).getName());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return view;
    }
}
